package com.ecc.common.utils;

import java.util.UUID;

import org.apache.commons.lang.math.NumberUtils;

/**   
 * @Description:TODO
 * @author: lxh 
 * @date: 2018年6月12日
 */
public class UUIDUtils {

	 //8
    private static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z" };

    //22
    private final static char[] DIGITS64 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_".toCharArray();


    public static String generateUuid8() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();
    }
    public static String generateUuid12() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        int random=((int)((Math.random() * 4 + 1) * 1000));
        shortBuffer.append(random);
        return shortBuffer.toString().toLowerCase();
    }

    public static String generateUuid32() {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        return uuid;
    }


    public static String generateUuid22() {
        UUID u = UUID.randomUUID();
        return toIDString(u.getMostSignificantBits()) + toIDString(u.getLeastSignificantBits());
    }

    private static String toIDString(long l) {
        char[] buf = "00000000000".toCharArray(); // 限定11位长度
        int length = 11;
        long least = 63L; // 0x0000003FL
        do {
            buf[--length] = DIGITS64[(int) (l & least)]; // l & least取低6位
            /* 无符号的移位只有右移，没有左移
             * 使用“>>>”进行移位
             * 为什么没有无符号的左移呢，知道原理的说一下哈
             */
            l >>>= 6;
        } while (l != 0);
        return new String(buf).replaceAll("-","");
    }
    public static String fillNumber3(String number_str){
        return fillNumber3(number_str,"001");
    }
    public static String fillNumber3(String number_str,String default_num){
        if(!NumberUtils.isNumber(number_str)){
            return default_num;
        }
        int number=NumberUtils.toInt(number_str)+1;
        if(number<10){
            return "00"+number;
        }else if(number>=0 && number<99){
            return "0"+number;
        }else if(number>=100 && number<999){
            return number+"";
        }
        return "";
    }

    public static String fillNumber2(String number_str){
        if(!NumberUtils.isNumber(number_str)){
            return "01";
        }
        int number=NumberUtils.toInt(number_str)+1;
        if(number<10){
            return "0"+number;
        }else if(number>=0 && number<99){
            return "0"+number;
        }
        return "";
    }

    public static String fillNumber8(String number_str){
        if(!NumberUtils.isNumber(number_str)){
            return "14080000";
        }
        int number=NumberUtils.toInt(number_str)+50;
        return number+"";
    }
    public static String fillNumberMerchant(String number_str){
        if(!NumberUtils.isNumber(number_str)){
            return "100000";
        }
        int number=NumberUtils.toInt(number_str)+1;
        return number+"";
    }

    public static String fillNumber(String number_str,int len){
        if(!NumberUtils.isNumber(number_str)){
            return number_str;
        }
        int number_str_len=number_str.length();
        if(number_str_len>=len){
            return number_str;
        }
        int sub_len = len-number_str_len;
        StringBuffer stringBuffer=new StringBuffer();
        for(int i=0;i<sub_len;i++){
            stringBuffer.append("0");
        }
        return stringBuffer.append(number_str).toString();
    }


    public static String sequence12(Long seq){
        String str="000000000000";
        int bit=seq.toString().length();
        return str.substring(0,12-bit)+seq;
    }




    public static void main(String[] args){
       System.out.println(fillNumber("222",5));
       System.out.println(sequence12(1l));
       System.out.println(sequence12(11l));
       System.out.println(sequence12(111l));
       System.out.println(sequence12(1111l));
       System.out.println(sequence12(11111l));
       System.out.println(sequence12(111111l));
       System.out.println(sequence12(1111111l));
       System.out.println(sequence12(11111111l));
       System.out.println(sequence12(111111111l));
       System.out.println(sequence12(1111111111l));
       System.out.println(sequence12(11111111111l));
       System.out.println(sequence12(111111111111l));
    }
}
